package Impl;

import Util.JDBCUtil;
import entity.User;
import Impl.UserDao;


import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class UserDaoImpl implements UserDao{
	Connection con = JDBCUtil.getConnection();
	PreparedStatement ps = null;
	ResultSet rs = null;
	User user = null;

	public User getUse_CommonCode(String sql,Object ...args){
		User tmp = null;
		try {
			con = JDBCUtil.getConnection();
			//预编译sql语句
			ps = con.prepareStatement(sql);
			for (int i = 0; i < args.length; i++) {
				ps.setObject(i + 1, args[i]);
			}
			//执行,并返回结果集
			rs = ps.executeQuery();
			if (rs.next()){
				tmp=new User();
				tmp.setId(rs.getInt("id"));
				tmp.setPhone(rs.getString("phone"));
				tmp.setEmail(rs.getString("email"));
				tmp.setName(rs.getString("name"));
				tmp.setPassword(rs.getString("password"));
				tmp.setImg_path(rs.getString("img_path"));
				tmp.setSex(rs.getBoolean("sex"));
				tmp.setReal_name(rs.getString("real_name"));
				tmp.setSchool(rs.getString("school"));
				tmp.setGrade(rs.getString("grade"));
			}else{
			    tmp=null;
            }
		}catch (SQLException e){
			e.printStackTrace();
		}
		return tmp;
	}

//通过邮箱或者电话选择用户
	@Override
	public User getUserByCondition(String condition) {
		String sql = "SELECT * FROM user u WHERE u.phone = ? or u.email=? ";
		user =getUse_CommonCode(sql,condition,condition);
		JDBCUtil.closeAll(con,ps,rs);
		return user;
	}

	//	注册
	@Override
	public void save(User user) {
		String sql = "INSERT INTO user(phone,email,password,name,province,city,area,img_path) VALUES(?,?,?,?,?,?,?,?)";
//		String sql = "INSERT INTO user(password,phone,email,name,school,grade) VALUES(?,?,?,?,?,?)";
		Object[] param = {user.getPhone(),user.getEmail(), user.getPassword(),user.getName(),user.getProvince(),user.getCity(),user.getArea(),user.getImg_path()};
		commonUp(sql, param);
		System.out.println("添加成功");
	}

//	更新
	@Override
	public void update(User user) {
		StringBuilder sql =new StringBuilder( "update user set phone=?,email=?,sex=?");
		ArrayList<Object> pList=new ArrayList<Object>();
		pList.add(user.getPhone());
		pList.add(user.getEmail());
		pList.add(user.getSex());
		if(user.getPassword()!=null){
			sql.append(",password=?");
			pList.add(user.getPassword());
		}
		if(user.getName()!=null){
			sql.append(",name=?");
			pList.add(user.getName());
		}
		if(user.getImg_path()!=null){
			sql.append(",img_path=?");
			pList.add(user.getImg_path());
		}
		if(user.getReal_name()!=null){
			sql.append(",real_name=?");
			pList.add(user.getReal_name());
		}
		if(user.getSchool()!=null){
			sql.append(",school=?");
			pList.add(user.getSchool());
		}
		if(user.getGrade()!=null){
			sql.append(",grade=?");
			pList.add(user.getGrade());
		}
		sql.append(" where id=?");
		pList.add(user.getId());
		Object[] param=pList.toArray();
		commonUp(sql.toString(), param);
	}

//删除
	@Override
	public void delete(int id) {
		String sql = "delete from user where id = ?";
		commonUp(sql,id);
		System.out.println("删除成功");
	}

//通用增删改代码
	public void commonUp(String sql,Object ...args){
		try {
			con=JDBCUtil.getConnection();
			ps = con.prepareStatement(sql);
			for (int i = 0; i <args.length ; i++) {
				ps.setObject(i+1,args[i]);
			}
			ps.execute();
			System.out.println("操作完成");
		} catch (SQLException e) {
			System.out.println("操作失败");
			e.printStackTrace();
		}finally {
			//关闭资源
			JDBCUtil.closeAll(con,ps,null);
		}
	}

//	注册创建用户通讯表
	public void create(User user){
		String sql ="create table "+user.getName()+" (fid int(5) AUTO_INCREMENT," +
				"fname varchar(255),fsex varchar(5),femail varchar(255)," +
				"fphone varchar(255),fschool varchar(255),PRIMARY KEY (`fid`))";
		System.out.println(sql);
		commonUp(sql);
		System.out.println("建表成功");
	}
/*
* 测试
* */
    public static void main(String[] args){
//		User u = new User("145632000","18211170102","9161488@126.com","DoDo");
//		UserDaoImpl userDao = new UserDaoImpl();
//		userDao.delete(2);
//		User u = userDao.getUserByCondition("15921219595");
//		System.out.println(u.getPhone());
//		User u = new User("987654","15906636106","910809089@qq.com","山楂树","温州大学");
//		userDao.save(u);
//		User tmp =userDao.test("18211170102");
//		System.out.println(tmp.getSchool());
//		userDao.create(u);


	}
}
